<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : editStatus?'查看':$t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" label-width="120px">
      <el-form-item prop="dcu" label="DCU编号">
        <el-input v-model="dataForm.dcu" :readonly="editStatus" placeholder="DUC编号"></el-input>
      </el-form-item>
	  <el-form-item prop="stationSide" label="站台侧">
	    <el-select v-model="dataForm.stationSide" :disabled="editStatus" placeholder="请选择">
	      <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
	    </el-select>
	  </el-form-item>
    <el-form-item prop="lineId" label="所属线路">
	   <el-select v-model="dataForm.lineId" :disabled="editStatus" placeholder="请选择" @change="changeLine">
	     <el-option v-for="item in options1" :key="item.lineId" :label="item.lineName" :value="item.lineId"></el-option>
	   </el-select>
	  </el-form-item>
	  <el-form-item prop="stationId" label="站台名称">
	    <!-- <el-input v-model="dataForm.stationName" placeholder="站台名称"></el-input> -->
      <el-select v-model="dataForm.stationId" :disabled="editStatus||dataForm.lineId==''" placeholder="请选择" >
        <el-option v-for="item in options2" :key="item.stationId" :label="item.stationName" :value="item.stationId"></el-option>
      </el-select>
	  </el-form-item>
    <el-form-item v-if="editStatus" prop="createDate" label="登记日期">
				<el-input v-model="dataForm.createDate" readonly placeholder="请输入线路名称"></el-input>
			</el-form-item>
			<el-form-item v-if="editStatus" prop="sysUserName" label="登记人">
				<el-input v-model="dataForm.sysUserName" readonly placeholder="请输入登记人"></el-input>
			</el-form-item>
    </el-form>
    <template slot="footer">
      <el-button v-if="!editStatus" @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button v-if="!editStatus" type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import utils from '@/utils/utils'
export default {
  data () {
    return {
      visible: false,
      editStatus: false,
      menuList: [],
      deptList: [],
      dataForm: {
        id: '',
        dcu: '',
        stationId:'',
        lineId:'',
        stationSide:'',
      },
	  options:utils.StationList,
	  options1:[],
    options2:[],
    }
  },
  computed: {
    dataRule () {
      return {
        dcu: [
          { required: true, message: 'DUC编号不能为空', trigger: 'blur' }
        ],
		stationId: [
          { required: true, message: '站台侧不能为空', trigger: 'blur' }
        ],
		lineId: [
          { required: true, message: '站台名称不能为空', trigger: 'blur' }
        ],
		stationSide: [
          { required: true, message: '所属线路不能为空', trigger: 'blur' }
        ],
      }
    }
  },
  mounted() {
    this.getLineId();
    this.getStationName();
  },
  methods: {
    init (status) {
      this.visible = true
      if(status){
        this.editStatus = status
      }else{
        this.editStatus = false
      }
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.getInfo()
          }
        
      })
    },
   getLineId(){
    this.$http.get('/psc/line/list').then((res) => {
      this.options1 = res.data.data
    })
   },
   getStationName(){
    this.$http.get('/psc/station/list').then((res) => {
      this.options2 = res.data.data
    })
   },
   changeLine(){
    this.$http.get(`/psc/station/sortList/${this.dataForm.lineId}`).then((res) => {
      this.options2 = res.data.data
    })
   },
    // 获取信息
    getInfo () {
      this.$http.get(`/psc/door/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        var obj = {
          doorId: this.dataForm.id,
          dcu: this.dataForm.dcu,
          stationId: this.dataForm.stationId,
          lineId: this.dataForm.lineId,
          stationSide: this.dataForm.stationSide
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/psc/door', obj).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
